﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 根据不同的品质等级，切换相应的共用材质球(sharedMaterial)
    /// </summary>
    [RequireComponent(typeof(Renderer))]
    public class QualityMaterialControl : MonoBehaviour
    {
        [Serializable]
        public struct ControlItem
        {
            /// <summary>
            /// 品质控制对象
            /// </summary>
            public Material Target;
            /// <summary>
            /// 控制对象在哪个品质等级显示
            /// </summary>
            public bool[] EnabledLevels;
        }

        [SerializeField]
        [Tooltip("The quality control items.")]
        private ControlItem[] controls;

        private Renderer mRenderer;
        private Material mOriginMaterial;

        private LinkedListNode<Action> onQualityChangedNode;

        private void Awake()
        {
            this.mRenderer = this.GetComponent<Renderer>();
            this.mOriginMaterial = this.mRenderer.sharedMaterial;

            onQualityChangedNode = QualityConfig.ListenQualityChanged(this.OnQualityLevelChanged);
            this.OnQualityLevelChanged();
        }

        private void OnDestroy()
        {
            if (this.onQualityChangedNode != null)
            {
                QualityConfig.UnlistenQualtiy(this.onQualityChangedNode);
                this.onQualityChangedNode = null;
            }
        }

#if UNITY_EDITOR
        private void Reset()
        {
            if (this.controls == null || this.controls.Length == 0)
            {
                var item = new ControlItem();
                item.Target = mOriginMaterial;
                item.EnabledLevels = new bool[] { false, true, true };
                this.controls = new ControlItem[] { item };
            }
        }

        public void SetQualityLevel(int level)
        {
            if (this.mRenderer == null) this.mRenderer = this.GetComponent<Renderer>();
            foreach (var ctrl in controls)
            {
                bool _enabled = false;
                if (level < ctrl.EnabledLevels.Length)
                {
                    _enabled = ctrl.EnabledLevels[level];
                }
                if (ctrl.Target != null && _enabled && this.mRenderer.sharedMaterial != ctrl.Target)
                {
                    this.mRenderer.sharedMaterial = ctrl.Target;
                }
            }
        }

#endif

        private void OnQualityLevelChanged()
        {
            var level = QualityConfig.QualityLevel;
            foreach (var ctrl in controls)
            {
                bool _enabled = false;
                if (level < ctrl.EnabledLevels.Length)
                {
                    _enabled = ctrl.EnabledLevels[level];
                }

                if (ctrl.Target != null && _enabled && this.mRenderer.sharedMaterial != ctrl.Target)
                {
                    this.mRenderer.sharedMaterial = ctrl.Target;
                }
            }
        }

        public bool CheckAndSetDefault()
        {
            if (this.mRenderer == null) this.mRenderer = this.GetComponent<Renderer>();

            bool isAnyChange = false;
            foreach (var ctrl in controls)
            {
                bool _enabled = ctrl.EnabledLevels[0];
                if (ctrl.Target != null && _enabled)
                {
                    var mat = ctrl.Target as Material;
                    if (mat != null && mRenderer.sharedMaterial != mat)
                    {
                        mRenderer.sharedMaterial = mat;
                        isAnyChange = true;
                    }
                }
            }
            return isAnyChange;
        }


    }
}

